package com.example.testcamara;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class PermissionsRequester {
    private final int mRequestCode = 100; //权限请求码
    public static boolean showSystemSetting = true;

    private void Permissions() {}

    private static PermissionsRequester pr;
    public static PermissionsRequester getInstance() {
        if (null == pr) {
            pr = new PermissionsRequester();
        }
        return pr;
    }

    private IPermissionsResult mPermissionsResult;
    public void getPermissions(Activity context, String[] perssions, @NonNull IPermissionsResult permissionsResult) {
        mPermissionsResult = permissionsResult;
        if (Build.VERSION.SDK_INT < 23) {
            mPermissionsResult.onResult(true);
            return;
        }
        List<String> permissionList = new ArrayList<>();
        for (int i = 0; i < perssions.length; ++i) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(context, perssions[i])) {
                permissionList.add(perssions[i]);
            }
        }
        if (permissionList.size() > 0) {
            ActivityCompat.requestPermissions(context, permissionList.toArray(new String[]{}), mRequestCode);
        } else {
            mPermissionsResult.onResult(true); // Failed
            return;
        }
    }

    public void onRequestPermissionsResult(Activity context, int requestCode,
                                           @NonNull String[] permissions, @NonNull int[] grantResults) {
        boolean dismiss = false;
        if (mRequestCode == requestCode) {
            for (int i = 0; i < grantResults.length; ++i) {
                if (-1 == grantResults[i]){
                    dismiss = true;
                }
            }
            if (dismiss){
                if (showSystemSetting){
                    showSystemPermissionsSettingDialog(context);
                } else {
                    mPermissionsResult.onResult(false);
                }
            } else {
                mPermissionsResult.onResult(true);
            }
        }
    }

    AlertDialog mPermissionDialog;
    private void showSystemPermissionsSettingDialog(final Activity context){
        final String mPackName = context.getPackageName();
        if (null == mPermissionDialog) {
            mPermissionDialog = new AlertDialog.Builder(context)
                    .setMessage("请手动授权权限")
                    .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            cancelPermissionDialog();
                            Uri packageURI = Uri.parse("package:" + mPackName);
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            context.startActivity(intent);
                            context.finish();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            cancelPermissionDialog();
                            mPermissionsResult.onResult(false);
                        }
                    })
                    .create();
        }
        mPermissionDialog.show();
    }

    private void cancelPermissionDialog(){
        if (null != mPermissionDialog) {
            mPermissionDialog.cancel();
            mPermissionDialog = null;
        }
    }

    interface IPermissionsResult {
        void onResult(boolean passPermissions);
    }

}
